package com.topsec.redis.redission;

import com.topsec.redis.redission.service.impl.RedissonDistributeLocker;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

@Configuration
public class RedissonConfig {

    @Value("${redisson-config}")
    private String redissonConfig;

    @Bean(destroyMethod="shutdown")
    public RedissonClient redisson() throws IOException {
        RedissonClient redisson = Redisson.create(
                Config.fromYAML(new ClassPathResource(redissonConfig).getInputStream()));
        return redisson;
    }

    @Bean
    public RedissonDistributeLocker redissonDistributeLocker(RedissonClient redissonClient){
        RedissonDistributeLocker locker = new RedissonDistributeLocker(redissonClient);
        //设置LockUtil的锁处理对象
        LockerUtil.setLocker(locker);
        return locker;
    }

}
